<script setup lang="ts">
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotApi } from '@/services/home'
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import { onLaunch, onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import HotPanel from './components/HotPanel.vue'

import PageSkeleton from './components/PageSkeleton.vue'
import { useGuessList } from '@/composables/index'

const bannerList = ref<BannerItem[]>([])

const categoryList = ref<CategoryItem[]>([])

const homehotList = ref<HotItem[]>([])

//获取轮播数据
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  console.log(res)
  bannerList.value = res.result
}

//获取前台分类数据
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}
//获取首页热门推荐
const getHomeHotData = async () => {
  const res = await getHomeHotApi()
  homehotList.value = res.result
}
//是否加载中标记
const isLoadin = ref(false)

onLoad(async () => {
  isLoadin.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  isLoadin.value = false
})
// //获取猜你喜欢
// const guessRef = ref<XtxGuessInstance>()
// //滚动触底
// const onScrolltolower = () => {
//   console.log('滚动触底了')
//   guessRef.value?.getMore()
// }

//猜你喜欢组件
const {guessRef,onScrolltolower} = useGuessList()

const isTriggered = ref(false)
//自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  //开启下拉刷新动画
  isTriggered.value = true

  //加载数据
  // await getHomeBannerData()
  // await getHomeCategoryData()
  // await getHomeHotData()
  //重置猜你喜欢组件数据
  guessRef.value?.resetData()
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  guessRef.value?.getMore()
  //关闭动画
  isTriggered.value = false
}
</script>

<template>
  <!-- 自定义导航栏 -->
  <CustomNavbar />
  <!-- 滚动容器 -->
  <scroll-view
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    @scrolltolower="onScrolltolower"
    :refresher-triggered="isTriggered"
    scroll-y
    class="scroll-view"
  >
    <PageSkeleton v-if="isLoadin" />
    <template v-else>
      <!-- 自定义轮播图 -->
      <XtxSwiper :list="bannerList" />
      <!-- 分类面版 -->
      <CategoryPanel :list="categoryList" />
      <!-- 熱門推薦 -->
      <HotPanel :list="homehotList" />
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
